package com.dyz.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Merge56 {
    public int[][] merge(int[][] intervals) {
        if(intervals.length==0){return intervals;}
        //排序
        Arrays.sort(intervals, new Comparator<int []>(){
            @Override
            public int compare(int[] a, int[] b) {
                return a[0] - b[0];
            }
        });

        //
        List<int []> res = new ArrayList<>();
        int [] cur = intervals[0];
        for (int i = 1; i<intervals.length;i++){
            if(intervals[i][0]>cur[1]){
                res.add(cur);
                cur = intervals[i];
            }else {
                cur[1] = Math.max(cur[1], intervals[i][1]);
            }
        }
        res.add(cur);
        int[][] ans = new int[res.size()][2];
        for(int i=0;i<res.size();i++){
            ans[i] = res.get(i);
        }
        return ans;
    }
}
